package GUI_Element;

import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;

import Kernel.Transition;

public class ShapeTransition extends Element {
	

	/* Its actual element */
	private Transition t;

	/* GUI element and its scale */
	Rectangle2D.Float rectangle=null;
	static final float SideLength = 30;	
	
	/* Graph Structure Exponent */
	private AbstractSet<ShapeArc> PreArcSet = new HashSet<ShapeArc>();
	private AbstractSet<ShapeArc> PostArcSet = new HashSet<ShapeArc>();
	
	/* Flag indicating if the transition is happening */
	private boolean isHappening=false;

	public ShapeTransition(float x1, float y1) {
		type = Element.TRANSITION;
		t = new Transition(this);
		this.x1 = x1;
		this.y1 = y1;
		rectangle=new Rectangle2D.Float(x1-SideLength/2, y1-SideLength/2, SideLength, SideLength);		
		shape = rectangle;
	}
	
	public ShapeTransition(ObjectInputStream ois) throws IOException {
		type = Element.TRANSITION;
		this.x1 = ois.readFloat();
		this.y1 = ois.readFloat();
		t = new Transition(ois,this);
		rectangle=new Rectangle2D.Float(x1-SideLength/2, y1-SideLength/2, SideLength, SideLength);		
		shape = rectangle;
	}

	public Element Clone() {
		return null;
	}

	public void SetHappening(boolean b){
		isHappening = b;
	}
	
	public void draw(Graphics2D g) {
		if (isSelected || isHappening)
			g.setColor(Element.SelectedColor);
		else
			g.setColor(Element.NormalColor);
		
		if (toBeDeleted)
			g.setColor(Element.DeleteColor);
	
		rectangle.x = x1-SideLength/2;
		rectangle.y = y1-SideLength/2;
		g.draw(shape);
		g.drawString("t"+t.TransitionIndex, x1-5, y1-SideLength/2-5);
	}
	
	public Transition GetActual() {
		return t;
	}
	
	public void AddPre(ShapeArc sa) {
		PreArcSet.add(sa);
	}
	public void AddPost(ShapeArc sa) {
		PostArcSet.add(sa);
	}
	public void RemovePre(ShapeArc sa){
		PreArcSet.remove(sa);
	}
	public void RemovePost(ShapeArc sa){
		PostArcSet.remove(sa);
	}
	
	public AbstractSet<ShapeArc> GetPreArcSet(){
		return this.PreArcSet;
	}
	public AbstractSet<ShapeArc> GetPostArcSet(){
		return this.PostArcSet;
	}

	public void Store(ObjectOutputStream oos) throws IOException{
		oos.writeFloat(x1);
		oos.writeFloat(y1);
		t.Store(oos);
	}
}
